package com.novel.common.event;


import com.novel.mapper.UserMapper;
import com.novel.pojo.novel.NovelComment;
import com.novel.pojo.system.User;
import com.novel.utils.FormatDateZone;
import com.novel.utils.GsonTool;
import com.novel.utils.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component
public class DeleteCommentListener {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MailUtils mailUtils;

    @EventListener
    public void process(DeleteCommentEvent event) {
        NovelComment comment = event.getNovelComment();

        String id = comment.getUserId();

        if (StringUtils.isBlank(id)) {
            log.error("该条评论用户id为空! 事件内容:{}", GsonTool.toJson(event));
            return;
        }

        User user = userMapper.selectByPrimaryKey(id);

        if (Objects.isNull(user)) {
            log.error("没有查询到对应的用户信息, 事件内容:{}", GsonTool.toJson(event));
            return;
        }

        mailUtils.sendEmail(user.getUserEmail(), "您的评论被删除了"
                , "您的评论,时间->" + DateFormatUtils.format(comment.getCreatedTime(), FormatDateZone.Char_Seconds)
                        + ",内容->" + comment.getCommentContent() + "被管理员/作者删除");

    }


}